<template>
  <el-form ref="formRef" :model="form" :rules="rules" label-width="140px" label-position="right">
    <el-form-item label="影像质量评估" prop="pass">
      <el-radio-group v-model="form.pass">
        <el-radio v-for="item in passOptions" :key="item.code" :label="item.code">{{
          item.value
        }}</el-radio>
      </el-radio-group>
    </el-form-item>

    <template v-if="state.show">
      <el-form-item label="说明" prop="reply">
        <el-input v-model="form.content" type="textarea" :autosize="{ minRows: 8 }" />
      </el-form-item>
      <el-form-item label="开始日期 - 截止日期">
        <el-date-picker
          v-model="state.dateRange"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="截止日期"
          value-format="YYYY-MM-DD"
        />
      </el-form-item>
      <el-form-item label="优先级" prop="priority">
        <el-select v-model="form.priority" style="width: 100%">
          <el-option
            v-for="item in priorityOptions"
            :key="item.code"
            :label="item.value"
            :value="item.code"
          />
        </el-select>
      </el-form-item>
    </template>
  </el-form>
</template>

<script lang="ts" setup>
import { defineExpose, reactive, ref, toRefs, watch } from 'vue'

import CommonState from './issuesState'
import IBaseObj from '#/index'
const { passOptions, priorityOptions, getTodayTime } = CommonState
const rules = {
  pass: [{ required: true, message: '请选择影像质量评估', trigger: 'change' }],
}
const state: IBaseObj = reactive({
  dateRange: [],
  show: false,
  form: {
    pass: '',
    priority: '',
    beginTime: '',
    endTime: '',
    content: '',
  },
})

const { form } = toRefs(state)

const formRef = ref()

watch(
  () => state.dateRange,
  (val) => {
    const [startDate, endDate] = val
    form.value.beginTime = startDate || ''
    form.value.endTime = endDate || ''
  }
)

watch(
  () => state.form.pass,
  (val) => {
    state.show = val === 102700 // 不符合才显示
  }
)

const getToday = () => {
  getTodayTime().then((today: string) => {
    state.dateRange = [today]
  })
}

getToday()

const reset = () => {
  state.form = {
    pass: '',
    priority: '',
    beginTime: '',
    endTime: '',
    content: '',
  }
  getToday()
}

defineExpose({
  form: state.form,
  reset,
})
</script>

<style lang="scss" scoped>
.el-form {
  padding: 20px;
  margin: 0 auto;

  :deep() {
    .el-form-item__content {
      width: 350px;
    }
  }
}
</style>
